﻿using BLL;
using System;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Business
{
    public partial class UserInfo : BaseUI.BasePage
    {
        private Sys_UserBLL userBLL = new Sys_UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var entity = userBLL.GetInfoById(CurrentUserID.ToString().Trim());
                BinRole(entity.USER_ROLEID);
                BindDepartment();

                FORM_LOGIN_NAME.Text = entity.USER_LOGINNAME;
                FORM_USER_NAME.Text = entity.USER_NAME;
                FORM_SEX.SelectedValue = entity.USER_SEX;
                FORM_PHONE.Text = entity.USER_PHONE;
                FORM_JOB_STATUS.SelectedValue = entity.USER_JOBSTATUS.Trim();// == "0" ? "离职" : "在职";
                FORM_LOGIN_DATE.Text = entity.USER_LOGINTIME.ToString("yyyy-MM-dd HH:mm:ss");
                FORM_COMPANY_NAME.Value = entity.USER_COMPANY;
                FORM_DEPARTMENT_NAME.SelectedValue = CurrentUserDeptID.ToString();//new Sys_DepartmentBLL().GetInfoById(CurrentUserDeptID.ToString().Trim()).DEPTNAME;
                txtIdCard.Text = entity.IDCardNumber;
                txtRemark.Text = entity.C_REMARK;
                drpLoginState.SelectedValue = entity.USER_LOGINSTATUS;
                drpOnlineState.SelectedValue = entity.USER_ONLINESTATUS;
            }
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Save_Click(object sender, EventArgs e)
        {
            try
            {
                var entity = userBLL.GetInfoById(CurrentUserID.ToString().Trim());
                entity.USER_ROLEID = FORM_ROLE_IDS.Value;
                entity.USER_SEX = FORM_SEX.SelectedValue;
                entity.USER_PHONE = FORM_PHONE.Text;
                entity.USER_NAME = FORM_USER_NAME.Text;
                entity.USER_JOBSTATUS = FORM_JOB_STATUS.SelectedValue;
                entity.USER_COMPANY = Convert.ToInt32(FORM_COMPANY_NAME.Value);
                entity.USER_DEPARTMENT = Convert.ToInt32(FORM_DEPARTMENT_NAME.Text);
                entity.IDCardNumber = txtIdCard.Text.Trim();
                entity.C_REMARK = txtRemark.Text.Trim();
                entity.USER_LOGINSTATUS = drpLoginState.SelectedValue;
                entity.USER_ONLINESTATUS = drpOnlineState.SelectedValue;
                if (userBLL.UPDATE(entity, CurrentUserName) > 0)
                {
                    MessageBox.Show(this, "【温馨提示】修改成功");
                }
                else
                {
                    MessageBox.Show(this, "【温馨提示】修改失败");
                }
            }
            catch (Exception)
            {
                MessageBox.Show(this, "【温馨提示】 发生异常");
            }
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_CLOSE();");
        }

        private void BindDepartment(decimal? id = null)
        {
            var dt = new Sys_DepartmentBLL().GetInfoALLCmyID(FORM_COMPANY_NAME.Value.ToString());

            FORM_DEPARTMENT_NAME.DataSource = dt;
            FORM_DEPARTMENT_NAME.DataTextField = "DEPTNAME";
            FORM_DEPARTMENT_NAME.DataValueField = "DEPTID";
            FORM_DEPARTMENT_NAME.DataBind();
            FORM_DEPARTMENT_NAME.Items.Insert(0, new ListItem("---请选择---", "-1"));
            FORM_DEPARTMENT_NAME.SelectedValue = id.ToString();
        }

        private void BinRole(string idStr)
        {
            var text = "";
            if (idStr != "")
            {
                text = new Sys_RoleBLL().GetRoleNames(idStr);
            }

            FORM_ROLE_IDS.Value = idStr;
            FORM_ROLE_STRING.Text = text;
        }
    }
}